package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.dao.BspsQrCodeInfoMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsQrCodeInfoMapperImpl;
import cc.rengu.igas.bsps.common.entity.QrCodeInfo;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.realize.PublicDealService;
import cc.rengu.igas.bsps.core.realize.impl.PublicDealServiceImpl;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.request.MchntQrCodeLogoImageUploadRequest;
import cc.rengu.igas.bsps.facade.response.MchntQrCodeLogoImageUploadResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import org.apache.commons.codec.binary.Base64;

/**
 * 上传二维码中心图标
 * Created by 王成 on 2020/3/23
 */
public class MchntQrCodeLogoImageUploadService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntQrCodeLogoImageUploadRequest mchntQrCodeLogoImageUploadRequest = new MchntQrCodeLogoImageUploadRequest();
        ConvertUtil.convertOutput(mchntQrCodeLogoImageUploadRequest);
        return mchntQrCodeLogoImageUploadRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        MchntQrCodeLogoImageUploadRequest mchntQrCodeLogoImageUploadRequest = (MchntQrCodeLogoImageUploadRequest) request;
        String instId = mchntQrCodeLogoImageUploadRequest.getHeader().getInstId();
        String mchntNo = mchntQrCodeLogoImageUploadRequest.getMchntNo();
        String userId = mchntQrCodeLogoImageUploadRequest.getUserId();
        /* 参数合法性检查 */
        if (StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(mchntNo) ||
                StringUtil.isEmptyOrNull(mchntQrCodeLogoImageUploadRequest.getLogoImagePath()) ||
                (StringUtil.isEmptyOrNull(mchntQrCodeLogoImageUploadRequest.getQrCode()) && StringUtil.isEmptyOrNull(mchntQrCodeLogoImageUploadRequest.getQrUrl()))) {
            rglog.error("接口上传参数不合法,userId:<{}>,mchntNo:<{}>,logoImagePath:<{}>,qrUrl:<{}>,qrCode:<{}>", mchntQrCodeLogoImageUploadRequest.getUserId(),
                    mchntQrCodeLogoImageUploadRequest.getMchntNo(), mchntQrCodeLogoImageUploadRequest.getLogoImagePath(), mchntQrCodeLogoImageUploadRequest.getQrUrl(), mchntQrCodeLogoImageUploadRequest.getQrCode());
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        /* 根据二维码URL或者二维码代码查询二维码信息 */
        QrCodeInfo qrCodeInfo;
        BspsQrCodeInfoMapper bspsQrCodeInfoMapper = new BspsQrCodeInfoMapperImpl();
        if (!StringUtil.isEmptyOrNull(mchntQrCodeLogoImageUploadRequest.getQrUrl())) {
            qrCodeInfo = bspsQrCodeInfoMapper.selectBspsQrCodeInfoByPrimaryKey(instId, mchntQrCodeLogoImageUploadRequest.getQrUrl());
        } else {
            qrCodeInfo = bspsQrCodeInfoMapper.selectBspsQrCodeInfoByQrCode(instId, mchntQrCodeLogoImageUploadRequest.getQrCode());
        }
        if (null == qrCodeInfo) {
            rglog.error("非法操作，二维码信息不存在!");
            throw new BizException(RespCodeEnum.RECORD_NOT_FOUND.getRespCode(), RespCodeEnum.RECORD_NOT_FOUND.getRespDesc());
        }
        PublicDealService publicDealService = new PublicDealServiceImpl();
        boolean opAuthFlag = publicDealService.checkUserOpAuth(instId, mchntNo, userId, qrCodeInfo.getUserId());
        if (!opAuthFlag) {
            rglog.error("用户无权解绑该二维码，用户标识:" + userId + "用户商户号：" + mchntNo + "二维码绑定收银员标识:" + qrCodeInfo.getUserId() + "二维码商户号:" + qrCodeInfo.getMchntNo());
            throw new BizException(RespCodeEnum.ILLEGAL_OPERATION.getRespCode(), RespCodeEnum.ILLEGAL_OPERATION.getRespDesc());
        }

        String path = new String(Base64.decodeBase64(mchntQrCodeLogoImageUploadRequest.getLogoImagePath()));
        /* 更新二维码中心图标地址 */
        qrCodeInfo.setLogoUrlPath(path);
        int iReturnCode = bspsQrCodeInfoMapper.updateBspsQrCodeInfoById(qrCodeInfo);
        if (0 != iReturnCode) {
            rglog.error("更新用户二维码中心LOGO失败,iReturnCode:<{}>", mchntQrCodeLogoImageUploadRequest.getLogoImagePath());
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }
        /* 拼装应答结果 */
        MchntQrCodeLogoImageUploadResponse mchntQrCodeLogoImageUploadResponse = new MchntQrCodeLogoImageUploadResponse();
        BizResponse<MchntQrCodeLogoImageUploadResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(mchntQrCodeLogoImageUploadResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntQrCodeLogoImageUploadResponse mchntQrCodeLogoImageUploadResponse = (MchntQrCodeLogoImageUploadResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntQrCodeLogoImageUploadResponse);
    }
}
